<template>
  <div id="setting">
    <el-tabs v-model="activeName" class="demo-tabs">
      <el-tab-pane label="基本信息" name="first">
        <ul class="baseinfo">
          <li>
            <div class="username-title">用户名</div>
            <div class="username" v-if="!isEdit">{{ baseInfo.username }}</div>
            <div class="username" v-else>
              <input type="text" v-model="baseInfo.username" />
            </div>
          </li>
          <li>
            <div class="sex-title">性别</div>
            <div class="sex" v-if="!isEdit">
              {{ baseInfo.sex == "man" ? "男" : "女" }}
            </div>
            <div class="sex" v-else>
              <label>
                <input
                  type="radio"
                  name="sex"
                  v-model="baseInfo.sex"
                  value="man"
                />
                <span>男</span>
              </label>
              <label>
                <input
                  type="radio"
                  name="sex"
                  v-model="baseInfo.sex"
                  value="woman"
                />
                <span>女</span>
              </label>
            </div>
          </li>
          <li>
            <div class="city-title">城市</div>
            <div class="city" v-if="!isEdit">{{ baseInfo.city }}</div>
            <div class="city" v-else>
              <input type="text" v-model="baseInfo.city" />
            </div>
          </li>
          <li>
            <div class="job-title">职位</div>
            <div class="job" v-if="!isEdit">{{ baseInfo.job }}</div>
            <div class="job" v-else>
              <input type="text" v-model="baseInfo.job" />
            </div>
          </li>
          <li>
            <div class="sign-title">签名</div>
            <div class="sign" v-if="!isEdit">{{ baseInfo.sign }}</div>
            <div class="sign" v-else>
              <input type="text" v-model="baseInfo.sign" />
            </div>
          </li>
          <li>
            <button class="edit" v-if="!isEdit" @click="edit">编辑</button>
            <button class="edit" v-else @click="save">保存</button>
            <button class="close" v-if="isEdit" @click="close">取消</button>
          </li>
        </ul>
      </el-tab-pane>
      <el-tab-pane label="账号设置" name="second">Config</el-tab-pane>
    </el-tabs>
  </div>
</template>

<script setup>
import { reactive, toRefs } from "vue";
import { useRouter } from "vue-router";
import { ref } from "vue";
const router = useRouter();

const activeName = ref("first");

const userBaseInfo = JSON.parse(localStorage.getItem("userInfo")) || {};

let baseInfo = reactive({});
baseInfo = userBaseInfo;

const isEdit = ref(false);

const edit = () => {
  isEdit.value = true;
};

const save = () => {
  localStorage.setItem("userInfo", JSON.stringify(baseInfo));
  isEdit.value = false;
};

const close = () => {
  isEdit.value = false;
};
</script>

<style lang="scss" scoped>
#setting {
  padding: 20px;
  ::v-deep .el-tabs__item.is-active,
  ::v-deep .el-tabs__item:hover {
    color: #f38e48;
  }
  ::v-deep .el-tabs__active-bar {
    background-color: #f38e48;
  }
  .baseinfo {
    padding: 20px;
    width: 600px;
    height: 520px;
    .edit,
    .close {
      height: 34px;
      width: 100px;
      line-height: 32px;
      background-color: #f38e48;
      color: white;
      border-radius: 5px;
      cursor: pointer;
      margin-right: 20px;
    }
    .close {
      background-color: #eee;
    }
    li {
      height: 90px;
      text-align: end;
      display: flex;
      input[type="text"] {
        outline: none;
        border: 1px solid #d7d7d7;
        border-radius: 4px;
        padding: 0 10px;
        width: 240px;
        color: #555;
        height: 30px;
      }
      input[type="radio"] ~ span {
        margin-right: 10px;
      }
      & > div:first-child {
        width: 10%;
      }
      &:last-child {
        height: 34px;
        text-align: center;
      }
      & > div:nth-of-type(2) {
        width: 80%;
        text-align: left;
        margin-left: 120px;
      }
    }
  }
}
</style>
